package cn.ac.cintcm.dataconverge.researcher.service.impl;

import java.util.List;

import cn.ac.cintcm.dataconverge.dao.IBaseDao;
import cn.ac.cintcm.dataconverge.researcher.dao.IGroupDao;
import cn.ac.cintcm.dataconverge.researcher.domain.Authgroup;
import cn.ac.cintcm.dataconverge.researcher.domain.Groups;
import cn.ac.cintcm.dataconverge.researcher.service.IGroupService;

import com.hs.core.page.PageParam;
import com.hs.core.page.PageUtil;
import com.hs.core.service.BaseSevice;

public class GroupService extends BaseSevice implements IGroupService{
	private IGroupDao groupDao;

	public IGroupDao getGroupDao() {
		return groupDao;
	}
	public List getAllGroups(){
		return this.groupDao.getAllGroups();
	}
	public void setGroupDao(IGroupDao groupDao) {
		this.groupDao = groupDao;
	}

	public PageUtil getAllGroups(PageParam pageParam) {
		// TODO Auto-generated method stub
		return groupDao.getAllGroups(pageParam);
	}

	public List getAllAuths() {
		// TODO Auto-generated method stub
		return groupDao.getAllAuths();
	}

	public boolean addGroup(Groups group, String[] rights) {
		// TODO Auto-generated method stub
		boolean rt=groupDao.getGroupByName(group.getGroupname());
		if(!rt)	{	
			groupDao.save(group);
		    for(int i=0;i<rights.length;i++){
		    	Authgroup authgp=new Authgroup();
		    	authgp.setAuthid(rights[i]);
		    	authgp.setGroupid(group.getGroupid());
		    	groupDao.save(authgp);
		    }		    
		return true;
		}else
			return false;
	}
	public List getGroupAuth(String groupids){
		return this.groupDao.getGroupAuth(groupids);
	}

	public List getGroupAuthByGroupid(Long groupid) {
		// TODO Auto-generated method stub
		return groupDao.getGroupAuthByGroupid(groupid);
	}

	public Groups getGroupByGroupid(Long groupid) {
		// TODO Auto-generated method stub
		return groupDao.getGroupByGroupid(groupid);
	}

	public void deleteGroupAuth(Long groupid) {
		// TODO Auto-generated method stub
		System.out.println("in the deleteGroupAuth:::::::::::::::::::::::::::::::::::::::::::::::::");
		List list=groupDao.getGroupAuth(groupid);
		
		System.out.println("list'size is::::::::::::::::::::::::::;;;;"+list.size());
		for(int i=0;i<list.size();i++){
			System.out.println(i);
			Authgroup authgp=(Authgroup)list.get(i);
			System.out.println(authgp);
		    groupDao.delete(authgp);
		}
		
	}

	public void updateGroup(Groups oldgroup, String[] rights) {
		this.deleteGroupAuth(oldgroup.getGroupid());
		for(int i=0;i<rights.length;i++){
	    	Authgroup authgp=new Authgroup();
	    	authgp.setAuthid(rights[i]);
	    	authgp.setGroupid(oldgroup.getGroupid());
	    	groupDao.save(authgp);
	    }
		groupDao.updateGroup(oldgroup);
		
	}

	public void deleteGroup(Long groupid) {
		groupDao.delete(groupDao.getGroupByGroupid(groupid));
		this.deleteGroupAuth(groupid);
		
	}

	public List getGroupByGroupidLow(Long group) {
		// TODO Auto-generated method stub
		return groupDao.getGroupByGroupidLow(group);
	}

	@Override
	public IBaseDao getBaseDao() {
		// TODO Auto-generated method stub
		return groupDao;
	}



}
